package service

import (
	"blog/app/models"
	"blog/config"
	"blog/utils"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
	"time"
)

// GetUserList 获取用户列表
func GetUserList(c *gin.Context) {
	var userModel []models.UserBasic
	err := utils.MySqlDB.Model(&userModel).Find(&userModel).Error
	if err != nil {
		fmt.Println("获取数据失败", err)
		CommonCJsonReply(c, config.ERROR, "获取数据失败")
		return
	}
	CommonCJsonReply(c, config.SUCCESS, userModel)
}

// ----------------------------- es 操作 和MySQL 操作 ----------------------------------//

var typeUserBasic = "user_basic2"

// AddUser 添加用户数据
func AddUser() {
	fmt.Println("添加用户数据")
	//var userModel []models.UserBasic
	for i := 20; i < 50; i++ {
		strNumber := strconv.Itoa(i)
		getName := "张三" + strNumber
		u := models.UserBasic{
			Model: gorm.Model{
				UpdatedAt: time.Now(),
				CreatedAt: time.Now(),
			},
			UserName:      getName,
			Password:      "123456",
			Phone:         "1312345678",
			Email:         strNumber + "@qq.com",
			Identity:      strNumber,
			ClientIP:      "1.1",
			ClientPort:    "6000",
			LoginTime:     time.Now(),
			HeartbeatTime: time.Now(),
			LogOutTime:    time.Now(),
			IsLogout:      false,
		}
		//fmt.Println(u)
		//fmt.Println("转换为字符串")

		kk, err := json.Marshal(u)
		if err != nil {
			fmt.Println("转换失败", err)
		}
		fmt.Println(string(kk))

		//utils.MySqlDB.Create(&u)
		//utils.AddEsDataString(typeUserBasic, strconv.Itoa(i), string(kk))
		//utils.ShowEsData(typeUserBasic, strconv.Itoa(i))
	}
	//CommonCJsonReply(c, config.SUCCESS, nil)
	fmt.Println(typeUserBasic)
}
